<?php
namespace common\libs\filemanager;
class filemanager
{
    public $basePath;
    public $rootPath;
    public $config;
    public $httpPath;

    function __construct()
    {
        $this->basePath = getcwd() . '\\files\\';
        $this->rootPath = 'files/';
        $this->config = '';
        $this->httpPath = '';
    }

    public function getList($dirPath = '')
    {
        $path = $this->basePath . $dirPath;
        $path = iconv("UTF-8", "gb2312", $path);
        if (!is_dir($path)) {
            return array("files" => array(), "dirs" => array());
        }

        $this->rootPath .= str_replace("\\", "/", $dirPath) . '/';

        $dirArray = array();
        $fileArray = array();
        $dir_obj = opendir($path);
        while ($dir = readdir($dir_obj)) {

            if ($dir != '.' && $dir != '..') {
                //return "/store/0-100/28//".$dir;
                $dir = iconv("gb2312", "utf-8", $dir);
                $filePath = $this->rootPath . $dir;  //str_replace("\\","/",$dir);
                // return $filePath;
                $filePath = iconv("utf-8", "gbk//IGNORE", $filePath);

                if (@is_file($filePath)) {
                    $size = filesize($filePath);
                    $fileArray[] = array(
                        'name' => $dir,
                        "length" => $size,
                        "path" => $this->rootPath . $dir,
                        "src" => $this->httpPath . $this->rootPath . $dir
                    );
                } else {
                    $dirArray[] = array(
                        "name" => $dir,
                        "path" => $this->rootPath . $dir
                    );
                }
            }
        }
        closedir($dir_obj);
        //echo json_encode(array("files" => $fileArray,"dirs"=>$dirArray));
        return array("files" => $fileArray, "dirs" => $dirArray);
    }

    public function getAllDirs($dirPath = '')
    {
        //return $this->config['dirs'];


        $path = $this->basePath . $dirPath;
        $path = iconv("UTF-8", "gb2312", $path);
        if (!is_dir($path)) {
            return array();
        }

        //$this->rootPath .=  str_replace("\\", "/", $dirPath) . '/';

        $dirArray = array();
        $dir_obj = opendir($path);
        while ($dir = readdir($dir_obj)) {

            if ($dir != '.' && $dir != '..') {
                $dir = iconv("gb2312", "utf-8", $dir);
                $filePath = $this->rootPath . $dirPath . $dir;  //str_replace("\\","/",$dir);
                $filePath = iconv("utf-8", "gbk//IGNORE", $filePath);
//if($dirPath=='store/0-100/28/')
//{ echo $filePath;exit;}
                if (!@is_file($filePath)) {

                    $children = $this->getAllDirs($dirPath . $dir . '/');
                    $dirArray[] = array(
                        "name" => $this->getDirName($dirPath . $dir, $dir),
                        "path" => $dirPath . $dir,
                        "map" => $this->getDirMapName($dirPath . $dir),
                        "children" => $children
                    );
                }
            }
        }
        closedir($dir_obj);
        return $dirArray;
    }

    private function getDirName($path, $name = "")
    {
        foreach ($this->config['dirs'] as $item) {
            if (strtolower($item['path']) == strtolower($path))
                $name = $item['name'];
        }
        return $name;
    }

    private function getDirMapName($path)
    {
        $name = '';
        $dir = '';
        $arrMap = explode('/', $path);
        foreach ($arrMap as $p) {
            if (empty($dir)) {
                $dir = $p;
                $flag = false;
                foreach ($this->config['dirs'] as $item) {
                    if (strtolower($item['path']) == strtolower($dir)) {
                        $name = $item['name'];
                        $flag = true;
                    }
                }
                if (!$flag)
                    $name = $p;
            } else {
                $dir .= '/' . $p;
                $flag = false;
                foreach ($this->config['dirs'] as $item) {
                    if (strtolower($item['path']) == strtolower($dir)) {
                        $name .= '/' . $item['name'];
                        $flag = true;
                    }
                }
                if (!$flag)
                    $name .= '/' . $p;
            }
        }
        return $name;
    }

    public function upload($dirPath)
    {
        $this->rootPath .= str_replace("\\", "/", $dirPath) . '/';
        $fileType = $this->getFileType($_FILES['file']['tmp_name']);

        $time = explode(" ", microtime());
        $time = $time [1] . ($time [0] * 1000000);
        $time2 = explode(".", $time);
        $time = $time2 [0];

        $receiveFile = $this->rootPath . $time . '.' . $fileType;
        move_uploaded_file($_FILES['file']['tmp_name'], $receiveFile);

        $result = array(
            'name' => '',
            "length" => 0,
            "path" => $receiveFile,
            "src" => $this->httpPath . $receiveFile
        );

        return $result;
    }

    private function getFileType($file)
    {
        $fp = fopen($file, "rb");
        $bin = fread($fp, 2); //只读2字节
        fclose($fp);
        $str_info = @unpack("C2chars", $bin);
        $type_code = intval($str_info['chars1'] . $str_info['chars2']);
        $file_type = '';
        switch ($type_code) {
            case 7790:
                $file_type = 'exe';
                break;
            case 7784:
                $file_type = 'midi';
                break;
            case 8075:
                $file_type = 'zip';
                break;
            case 8297:
                $file_type = 'rar';
                break;
            case 255216:
                $file_type = 'jpg';
                break;
            case 7173:
                $file_type = 'gif';
                break;
            case 6677:
                $file_type = 'bmp';
                break;
            case 13780:
                $file_type = 'png';
                break;
            default:
                $file_type = 'unknown';
                break;
        }

        return $file_type;
    }
}

?>